import { call, fork, take, put } from 'redux-saga/effects'
import actions,{ LOGOUT, GET_USER_INFO } from '@/actions/layout'
import api from '@/api/layout'
import apiIndSetting from '@/api/individuationSetting'
import { history } from '@/route/history'

export function* logout() {
  try {
    const res = yield call(api.logout)
    history.push('/')
  } catch (error) {
    console.log('logout error')
  }
}

function* getUserInfo() {
  try {
    const { isSuccess, data } = yield call(apiIndSetting.getUserInfo)
    if(isSuccess && data){
      yield put(actions.getUserInfoSuccess(data))
    }
  } catch (error) {
  }
}

function* watchGetUserInfo() {
  while (true) {
    yield take(GET_USER_INFO)
    yield call(getUserInfo)
  }
}

function* watchLogout() {
  while (true) {
    yield take(LOGOUT)
    yield call(logout)
  }
}

export default [
  fork(watchLogout),
  fork(watchGetUserInfo),
]
